﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AtletIS.DomainModel.DTOs;
using AtletIS.DomainModel.Entities.Main;
using AtletIS.Repository.UnitOfWork;
using AtletIS.Service.Helpers;

namespace AtletIS.Service.Services
{
    public class ResultsService
    {

        #region Results HTML string

        #endregion

        #region Results collection

        /// <summary>
        ///  Vráti kolekciu dát pre výsledky
        /// </summary>
        public List<ResultsDto> GetResultsCollection(int disciplineListId, out int totalCount)
        {
            DisciplineList disciplineList = new DisciplineListService().GetById(disciplineListId);

            if (disciplineList != null)
            {
                return GetResultsCollection(disciplineList, out totalCount);
            }

            totalCount = 0;
            return null;
        }

        /// <summary>
        ///  Vráti kolekciu dát pre výsledky
        /// </summary>
        public List<ResultsDto> GetResultsCollection(DisciplineList disciplineList, out int totalCount)
        {
            List<ResultsDto> results = new List<ResultsDto>();

            List<Performance> performances = new PerformancesService()
                .GetByDisciplineListId(disciplineList.ID, Enums.PerformancesSortEnum.Results, out totalCount);

            if (performances != null && performances.Count > 0)
            {
                foreach (Performance performance in performances)
                {
                    ResultsDto resultsDto = this.ConvertPerformanceToResultsDto(performance, disciplineList);
                    results.Add(resultsDto);
                }
            }

            return results;
        }

        /// <summary>
        ///  Konvertuje Performance na ResultsDto
        /// </summary>
        private ResultsDto ConvertPerformanceToResultsDto(Performance performance, DisciplineList disciplineList)
        {
            ResultsDto resultsDto = new ResultsDto();

            // Rank
            if (performance.Rank.HasValue && performance.Rank > 0)
            {
                resultsDto.Rank = performance.Rank.Value.ToString();
            }
            else
            {
                resultsDto.Rank = string.Empty;
            }
            
            // Start number
            if (performance.StartNumber.HasValue && performance.StartNumber > 0)
            {
                resultsDto.StartNumber = performance.StartNumber.Value.ToString();
            }
            else
            {
                resultsDto.StartNumber = string.Empty;
            }
            
            // FullName
            resultsDto.FullName = string.Format("{0} {1}", performance.Surname.ToUpper(), performance.FirstName);

            // BirthDate
            if (performance.Birthdate.HasValue)
            {
                resultsDto.BirthDate = PrintHelper.PrintDate(performance.Birthdate.Value);
            }
            else
            {
                resultsDto.BirthDate = string.Empty;
            }

            // Country
            resultsDto.CountryId = performance.CountryId;
            if (performance.Country != null)
            {
                resultsDto.Country = performance.Country.Name;
            }

            // Club
            resultsDto.Club = performance.Club;

            // Result
            resultsDto.Result = PrintHelper.PrintResult(performance.Result.Value.ToString(), disciplineList.DisciplineCode.Value);

            #region ExtTexts

            if (performance.ExtTexts != null && performance.ExtTexts.Count > 0)
            {
                resultsDto.ExtTexts.Clear();

                foreach (ExtText extText in performance.ExtTexts)
                {
                    ExtTextDto extTextDto = new ExtTextDto()
                    {
                        Header = extText.Header
                    };

                    // Value
                    if (!String.IsNullOrEmpty(extText.Text1))
                    {
                        extTextDto.Value = extText.Text1;
                    }
                    else
                    {
                        extTextDto.Value = string.Empty;
                    }

                    // Value2
                    if (!String.IsNullOrEmpty(extText.Text2))
                    {
                        extTextDto.Value2 = extText.Text2;
                    }
                    else
                    {
                        extTextDto.Value2 = string.Empty;
                    }
                    
                    resultsDto.ExtTexts.Add(extTextDto);
                }
            }

            #endregion

            return resultsDto;
        }

        #endregion

    }
}
